﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using Point = System.Windows.Point;

namespace SmartOCR
{
    /// <summary>
    /// MaskWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MaskWindow : Window
    {
        private Rect cropRect = new Rect();

        public delegate void SetCropRect(Rect rect);

        public SetCropRect setCropRect;

        public MaskWindow()
        {
            InitializeComponent();

            this.MouseLeftButtonDown += MaskWindow_MouseLeftButtonDown;
            this.MouseMove += MaskWindow_MouseMove;
            this.MouseLeftButtonUp += MaskWindow_MouseLeftButtonUp;
            this.MouseDoubleClick += MaskWindow_MouseDoubleClick;
        }

        private void MaskWindow_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            this.Close();

            //maskCanvas.drawRect(cropRect);

            //Application.Current.MainWindow.Show();

            Debug.WriteLine("cropRect: x=" + cropRect.X + ",y=" + cropRect.Y + ",w=" + cropRect.Width + ",h=" + cropRect.Height);
            double xRatio = SystemParameters.PrimaryScreenWidth / ActualWidth;
            double yRatio = SystemParameters.PrimaryScreenHeight / ActualHeight;
            Rect realRect = new Rect(cropRect.X * xRatio, cropRect.Y * xRatio, cropRect.Width * yRatio, cropRect.Height * yRatio);

            setCropRect(realRect);
        }


        #region 出现区域
        private Point oldMousePosition;
        void MaskWindow_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            fe.ReleaseMouseCapture();
            e.Handled = true;
        }

        void MaskWindow_MouseMove(object sender, MouseEventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            if (e.LeftButton == MouseButtonState.Released || !fe.IsMouseCaptured) return;

            Point mousePosition = e.GetPosition(this);
            Vector spacing = mousePosition - oldMousePosition;//两次鼠标移动的间距
            oldMousePosition = mousePosition;

            double newWidth = border.Width + spacing.X;
            double newHeight = border.Height + spacing.Y;

            if (newWidth > 0 && newHeight > 0)
            {
                border.Width = newWidth;
                border.Height = newHeight;

                cropRect.Width += spacing.X;
                cropRect.Height += spacing.Y;

                Refresh();
            }

            e.Handled = true;
        }

        void MaskWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            oldMousePosition = e.GetPosition(this);

            double x = oldMousePosition.X;
            double y = oldMousePosition.Y;

            border.Width = border.Height = 0;

            Canvas.SetLeft(border, x);
            Canvas.SetTop(border, y);

            // 点击在已选区域内，可能是双击该选区，不更新选区信息，该选区将作为有效区域
            if (!cropRect.Contains(x, y))
            {
                cropRect.X = x;
                cropRect.Y = y;
                cropRect.Width = 0;
                cropRect.Height = 0;
            }

            fe.CaptureMouse();
            e.Handled = true;
        }
        #endregion

        #region Event Handler
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Refresh();
        }

        private void Refresh()
        {
            maskCanvas.Refresh(border.Width, border.Height, Canvas.GetLeft(border), Canvas.GetTop(border));
        }
        #endregion
    }
}
